﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Threading;
using Active.Core;
using Active.Core.State;

public partial class Bill_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ApplyAuthorizationRules();
            BindYears();
        }
    }

    private void BindYears()
    {
        //绑定年
        for (int i = 2007; i <= DateTime.Now.Year; i++)
        {
            ListItem li = new ListItem(i.ToString(), i.ToString());
            ddlYear.Items.Add(i.ToString());
        }
        ddlYear.Items.Add((DateTime.Now.Year + 1).ToString());
        ListItem allYear = new ListItem("所有年份", "0");
        ddlYear.Items.Add(allYear);
        ddlYear.SelectedValue = "0";
        //ddlYear.SelectedValue = DateTime.Now.Year.ToString();
    }

    private void ApplyAuthorizationRules()
    {
        if (Csla.ApplicationContext.User.IsInRole("MediaEmployee"))
        {
            Response.Redirect("~/Bill/OrderList.aspx", true);
        }
    }

    protected void aspnetPager_PageChanged(object sender, EventArgs e)
    {
        BillsGridView.DataBind();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BillsGridView.DataBind();
    }

    #region Business Methods

    private BillList GetBillList()
    {
        //清除Session中的业务对象, 避免出现Session保存错误的业务对象
        if (Session["CurrentObj"] != null)
        {
            Session["CurrentObj"] = null;
        }
        int year = int.Parse(ddlYear.SelectedValue);
        int month = int.Parse(ddlMonth.SelectedValue);
        int pageNo = aspnetPager.CurrentPageIndex;
        int pageSize = aspnetPager.PageSize;
        int customerId = 0;
        int.TryParse(Request.QueryString["customerId"], out customerId);
        BillList list = null;
        try
        {
            list = BillList.GetBillList(customerId, year, month, pageNo, pageSize);
            aspnetPager.RecordCount = list.TotalRecordsCount;
        }
        catch (System.Security.SecurityException)
        {
            Response.Redirect("~/");
        }
        return list;
    }

    #endregion

    #region BillsDataSource

    protected void BillsDataSource_SelectObject(object sender, Csla.Web.SelectObjectArgs e)
    {
        e.BusinessObject = GetBillList();
    }

    #endregion

}
